/***********************************************************
 *  File_Name  : XJStringUtil.cpp
 *  File_Path   : 
 *  File_Encode : UTF8
 *  Description : 字段串相关函数
 *  
 *  OS : Linux, UNIX
 *  LastVersion  : 20130117
 *      
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2013-1-17
 *  Version     : 20130117
 *  Description : 
 *                
 *  Edit Author : 
 *  Edit Date   : 
 *  Version     : 
 *  Description :
 *                
 ***********************************************************/
#include "XJStringUtil.h"
namespace XJUtilClass{
XJStringUtil::XJStringUtil() {
	// TODO Auto-generated constructor stub

}

XJStringUtil::~XJStringUtil() {
	// TODO Auto-generated destructor stub
}
vector<string> &XJStringUtil::Split(vector<string> &veStr, const string &strSource,
		const string &strSeparator){
	unsigned long nBegin = 0, nPos = 0;
	string strSubStr;
	while((nPos = strSource.find(strSeparator, nBegin)) != string::npos){
		strSubStr = strSource.substr(nBegin, nPos-nBegin);
		veStr.push_back(strSubStr);
		nBegin = nPos + strSeparator.size();
		if (nBegin > strSource.size())
			break;
	}
	if (nBegin <= strSource.size()){
		strSubStr = strSource.substr(nBegin);
		veStr.push_back(strSubStr);
	}
	return veStr;
}
string &XJStringUtil::TrimHeadTail(string &strSource, const char ch){
	string strTrim(1, ch);
	strSource.erase(strSource.find_last_not_of(strTrim)+1); // 标准string +1
	strSource.erase(0, strSource.find_first_not_of(strTrim));
	return strSource;
}
string &XJStringUtil::Replace(string &strSource, const string &strFindStr,
		const string &strReplace){
	unsigned long nBegin = 0, nPos = 0;
	while (string::npos != (nPos = strSource.find(strFindStr, nBegin))){
		strSource.erase(nPos, strFindStr.size());
		strSource.insert(nPos, strReplace);
		nBegin = nPos + strFindStr.size();
		if (strFindStr.size() > strReplace.size()){
			nBegin = nBegin - (strFindStr.size() - strReplace.size());
		}else{
			nBegin = nBegin + (strReplace.size() - strFindStr.size());
		}
		if (nBegin >= strSource.size()){
			break;
		}
	}
	return strSource;
}
vector<string> &XJStringUtil::FindSubstr(vector<string> &veStr, const string &strSource,
		const string &strFindBegin, const string &strFindEnd,
		const bool bIsIncludeFindStr /* = false */, const unsigned long ulFindIndex /* =0 */,
		const unsigned long ulFindSize /* = string::npos */){
	string strSubStr;
	unsigned long nPosBegin = 0, nPosEnd = string::npos, nIndex = 0, i = 0;
	nPosBegin = strSource.find(strFindBegin, nIndex);
	if (nPosBegin != string::npos){
		nPosEnd = strSource.find(strFindEnd, nPosBegin + strFindBegin.size());
	}
	while (nPosEnd != string::npos){
		if (i >= ulFindIndex && i - ulFindIndex < ulFindSize){
			strSubStr = strSource.substr(nPosBegin + strFindBegin.size(),
					nPosEnd - (nPosBegin + strFindBegin.size()));
			if (bIsIncludeFindStr) {
				strSubStr.insert(0, strFindBegin);
				strSubStr.append(strFindEnd);
			}
			veStr.push_back(strSubStr);
		}
		++i;
		nIndex = nPosEnd + strFindEnd.size();
		if (nIndex > strSource.size()){
			break;
		}
		nPosBegin = strSource.find(strFindBegin, nIndex);
		if (nPosBegin != string::npos){
			nPosEnd = strSource.find(strFindEnd, nPosBegin + strFindBegin.size());
		}else{
			break;
		}
	}

	return veStr;
}
string &XJStringUtil::Uppercase(string &strSource){
	unsigned long i = 0;
	for (i=0;i < strSource.size();++i){
		strSource.replace(i, 1, 1, toupper(strSource.at(i)));
	}
	return strSource;
}
string &XJStringUtil::Lowercase(string &strSource){
	unsigned long i = 0;
	for (i=0;i < strSource.size();++i){
		strSource.replace(i, 1, 1, tolower(strSource.at(i)));
	}
	return strSource;
}
string XJStringUtil::FromLong(long lNum){
	char szData[32] = { 0x00 };
	sprintf(szData, "%ld", lNum);
	return string(szData);
}
string XJStringUtil::FromDouble(double dNum, unsigned long nLen, bool bRoundingOff){
	char szData[64] = { 0x00 };
	char szTmp[64] = { 0x00 };
	sprintf(szTmp, "0%f", dNum);
	unsigned long i = 0;
	char *pPoint = NULL, *p = NULL;
	pPoint = strstr(szTmp, ".");
	if (0 == nLen){
		if (pPoint != NULL)
			*pPoint= '\0';
	}else if (nLen < 16){
		if (NULL == pPoint){
			szTmp[strlen(szTmp)] = '.';
			pPoint = &szTmp[strlen(szTmp) - 1];
		}
		p = pPoint + nLen + 1;
		*(p+1) = '\0';
		if (p > pPoint){
			if ('\0' == *p)
				*p = '0';
			--p;
		}
		if (bRoundingOff){
			i = 1;
			while (szTmp[strlen(szTmp) - i] == '9'){
				szTmp[strlen(szTmp) - i] = '0';
				++i;
				if ('.' == szTmp[strlen(szTmp) - i]){
					++i;
				}
			}
			++szTmp[strlen(szTmp) - i];
		}
		szTmp[strlen(szTmp) - 1] = '\0';
	}
	if ('0' == szTmp[0])
		strcpy(szData, szTmp+1);
	else
		strcpy(szData, szTmp);
	return string(szData);
}
string XJStringUtil::FromLongLong(long long llNum){
	char szData[64] = { 0x00 };
	sprintf(szData, "%lld", llNum);
	return string(szData);
}
string XJStringUtil::FromLongDouble(long double ldNum,unsigned long nLen,bool bRoundingOff){
	char szData[64] = { 0x00 };
	char szTmp[64] = { 0x00 };
	sprintf(szTmp, "0%Lf", ldNum);
	unsigned long i = 0;
	char *pPoint = NULL, *p = NULL;
	pPoint = strstr(szTmp, ".");
	if (0 == nLen){
		if (p != NULL)
			*pPoint= '\0';
	}else if (nLen < 64){
		if (NULL == pPoint){
			szTmp[strlen(szTmp)] = '.';
			pPoint = &szTmp[strlen(szTmp) - 1];
		}
		p = pPoint + nLen + 1;
		*(p+1) = '\0';
		if (p > pPoint){
			if ('\0' == *p)
				*p = '0';
			--p;
		}
		if (bRoundingOff){
			i = 1;
			while (szTmp[strlen(szTmp) - i] == '9'){
				szTmp[strlen(szTmp) - i] = '0';
				++i;
				if ('.' == szTmp[strlen(szTmp) - i]){
					++i;
				}
			}
			++szTmp[strlen(szTmp) - i];
		}
		szTmp[strlen(szTmp) - 1] = '\0';
	}
	if ('0' == szTmp[0])
		strcpy(szData, szTmp+1);
	else
		strcpy(szData, szTmp);
	return string(szData);
}
string XJStringUtil::FromULong(unsigned long lNum){
	char szData[32] = { 0x00 };
	sprintf(szData, "%lu", lNum);
	return string(szData);
}

string XJStringUtil::FromULongLong(unsigned long long llNum){
	char szData[64] = { 0x00 };
	sprintf(szData, "%llu", llNum);
	return string(szData);
}

short XJStringUtil::ToShort(const string &strNum){
	unsigned long i;
	short n = 0;
	if (IsShort(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
int XJStringUtil::ToInt(const string &strNum){
	unsigned long i;
	int n = 0;
	if (IsInt(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
long XJStringUtil::ToLong(const string &strNum){
	unsigned long i;
	long n = 0;
	if (IsLong(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
float XJStringUtil::ToFloat(const string &strNum){
	if (IsFloat(strNum)){
		return static_cast<float>(atof(strNum.c_str()));
	}else
		return 0.00;
}
double XJStringUtil::ToDouble(const string &strNum){
	if (IsDouble(strNum)){
		return atof(strNum.c_str());
	}else
		return 0.00;
}
long long XJStringUtil::ToLongLong(const string &strNum){
	unsigned long i;
	long long n = 0;
	if (IsLongLong(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
long double XJStringUtil::ToLongDouble(const string &strNum){
	long double ldNum = 0;
	stringstream ss;
	ss << strNum;
	ss >> ldNum;
	return ldNum;
}
unsigned short XJStringUtil::ToUShort(const string &strNum){
	unsigned long i;
	unsigned short n = 0;
	if (IsUShort(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
unsigned int XJStringUtil::ToUInt(const string &strNum){
	unsigned long i;
	unsigned int n = 0;
	if (IsUInt(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
unsigned long XJStringUtil::ToULong(const string &strNum){
	unsigned long i;
	unsigned long n = 0;
	if (IsULong(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
unsigned long long XJStringUtil::ToULongLong(const string &strNum){
	unsigned long i;
	unsigned long long n = 0;
	if (IsULongLong(strNum)){
		for (i=0;i < strNum.size();++i){
			n = n*10 + (strNum.at(i) - '0');
		}
		return n;
	}else
		return 0;
}
bool XJStringUtil::IsNum(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i < strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			return false;
		}
	}
	return true;
}
bool XJStringUtil::IsShort(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			return false;
		}
	}
	const char szInt16_Max[] = "32767";
	const char szInt16_Min[] = "-32768";
	if (strNum.at(0) != '-'){
		if (strNum.size() > strlen(szInt16_Max)){
			return false;
		}else if (strNum.size() == strlen(szInt16_Max)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt16_Max[i]){
					continue;
				}else if (strNum.at(i) > szInt16_Max[i]){
					return false;
				}else {
					break;
				}
			}
		}
	} else {
		if (strNum.size() > strlen(szInt16_Min))
			return false;
		else if (strNum.size()  == strlen(szInt16_Min)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt16_Min[i]){
					continue;
				}else if (strNum.at(i) > szInt16_Min[i]){
					return false;
				}else {
					break;
				}
			}
		}
	}
	return true;
}
bool XJStringUtil::IsInt(const string &strNum){
	if (sizeof(int) == sizeof(short)){
		return IsShort(strNum);
	}else{
		if (strNum.size() == 0){
			return false;
		}
		unsigned long i;
		for (i=0;i<strNum.size();++i){
			if (strNum.at(i) < '0' || strNum.at(i) > '9'){
				if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
					continue;
				}
				return false;
			}
		}
		const char szInt32_Max[] = "2147483647";
		const char szInt32_Min[] = "-2147483648";
		if (strNum.at(0) != '-'){
			if (strNum.size() > strlen(szInt32_Max)){
				return false;
			}else if (strNum.size() == strlen(szInt32_Max)){
				for (i=0;i<strNum.size();++i){
					if (strNum.at(i) == szInt32_Max[i]){
						continue;
					}else if (strNum.at(i) > szInt32_Max[i]){
						return false;
					}else {
						break;
					}
				}
			}
		} else {
			if (strNum.size() > strlen(szInt32_Min))
				return false;
			else if (strNum.size()  == strlen(szInt32_Min)){
				for (i=0;i<strNum.size();++i){
					if (strNum.at(i) == szInt32_Min[i]){
						continue;
					}else if (strNum.at(i) > szInt32_Min[i]){
						return false;
					}else {
						break;
					}
				}
			}
		}
		return true;
	}
	return false;
}
bool XJStringUtil::IsLong(const string &strNum){
	if (sizeof(long ) == sizeof(long long )){
		return IsLongLong(strNum);
	}
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			return false;
		}
	}
	const char szInt32_Max[] = "2147483647";
	const char szInt32_Min[] = "-2147483648";
	if (strNum.at(0) != '-'){
		if (strNum.size() > strlen(szInt32_Max)){
			return false;
		}else if (strNum.size() == strlen(szInt32_Max)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt32_Max[i]){
					continue;
				}else if (strNum.at(i) > szInt32_Max[i]){
					return false;
				}else {
					break;
				}
			}
		}
	} else {
		if (strNum.size() > strlen(szInt32_Min))
			return false;
		else if (strNum.size()  == strlen(szInt32_Min)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt32_Min[i]){
					continue;
				}else if (strNum.at(i) > szInt32_Min[i]){
					return false;
				}else {
					break;
				}
			}
		}
	}
	return true;
}
bool XJStringUtil::IsFloat(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	unsigned long nIntLen = 0;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			if ('.' == strNum.at(i)){
				nIntLen = i;
				continue;
			}
			return false;
		}
	}
	if (nIntLen > 7)
		return false;
	return true;
}
bool XJStringUtil::IsDouble(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	unsigned long nIntLen = 0;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			if ('.' == strNum.at(i)){
				nIntLen = i;
				continue;
			}
			return false;
		}
	}
	if (nIntLen > 16)
		return false;
	return true;
}
bool XJStringUtil::IsLongLong(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}

	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			return false;
		}
	}
	const char szInt64_Max[] = "9223372036854775807";
	const char szInt64_Min[] = "-9223372036854775808";
	if (strNum.at(0) != '-'){
		if (strNum.size() > strlen(szInt64_Max)){
			return false;
		}else if (strNum.size() == strlen(szInt64_Max)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt64_Max[i]){
					continue;
				}else if (strNum.at(i) > szInt64_Max[i]){
					return false;
				}else {
					break;
				}
			}
		}
	} else {
		if (strNum.size() > strlen(szInt64_Min))
			return false;
		else if (strNum.size()  == strlen(szInt64_Min)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szInt64_Min[i]){
					continue;
				}else if (strNum.at(i) > szInt64_Min[i]){
					return false;
				}else {
					break;
				}
			}
		}
	}
	return true;
}
bool XJStringUtil::IsLongDouble(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	unsigned long nIntLen = 0;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			if ('-' == strNum.at(i) && 0 == i && strNum.size() > 1){
				continue;
			}
			if ('.' == strNum.at(i)){
				nIntLen = i;
				continue;
			}
			return false;
		}
	}
	if (nIntLen > 32)
		return false;
	return true;
}
bool XJStringUtil::IsUShort(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			return false;
		}
	}
	const char szUInt16_Max[] = "65535";
	if (strNum.size() > strlen(szUInt16_Max)){
		return false;
	}else if (strNum.size() == strlen(szUInt16_Max)){
		for (i=0;i<strNum.size();++i){
			if (strNum.at(i) == szUInt16_Max[i]){
				continue;
			}else if (strNum.at(i) > szUInt16_Max[i]){
				return false;
			}else {
				break;
			}
		}
	}
	return true;
}
bool XJStringUtil::IsUInt(const string &strNum){
	if (sizeof(int) == sizeof(short)){
		return IsUShort(strNum);
	}else{
		if (strNum.size() == 0){
			return false;
		}
		unsigned long i;
		for (i=0;i<strNum.size();++i){
			if (strNum.at(i) < '0' || strNum.at(i) > '9'){
				return false;
			}
		}
		const char szUInt32_Max[] = "4394967295";
		if (strNum.size() > strlen(szUInt32_Max)){
			return false;
		}else if (strNum.size() == strlen(szUInt32_Max)){
			for (i=0;i<strNum.size();++i){
				if (strNum.at(i) == szUInt32_Max[i]){
					continue;
				}else if (strNum.at(i) > szUInt32_Max[i]){
					return false;
				}else {
					break;
				}
			}
		}
		return true;
	}
	return false;
}
bool XJStringUtil::IsULong(const string &strNum){
	if (sizeof(unsigned long ) == sizeof(unsigned long long )){
		return IsULongLong(strNum);
	}
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			return false;
		}
	}
	const char szUInt32_Max[] = "4394967295";
	if (strNum.size() > strlen(szUInt32_Max)){
		return false;
	}else if (strNum.size() == strlen(szUInt32_Max)){
		for (i=0;i<strNum.size();++i){
			if (strNum.at(i) == szUInt32_Max[i]){
				continue;
			}else if (strNum.at(i) > szUInt32_Max[i]){
				return false;
			}else {
				break;
			}
		}
	}
	return true;
}
bool XJStringUtil::IsULongLong(const string &strNum){
	if (strNum.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strNum.size();++i){
		if (strNum.at(i) < '0' || strNum.at(i) > '9'){
			return false;
		}
	}
	const char szUInt64_Max[] = "1844674407370955161";
	if (strNum.size() > strlen(szUInt64_Max)){
		return false;
	}else if (strNum.size() == strlen(szUInt64_Max)){
		for (i=0;i<strNum.size();++i){
			if (strNum.at(i) == szUInt64_Max[i]){
				continue;
			}else if (strNum.at(i) > szUInt64_Max[i]){
				return false;
			}else {
				break;
			}
		}
	}
	return true;
}
bool XJStringUtil::Isaz(const string &strSource){
	if (strSource.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strSource.size();++i){
		if (strSource.at(i) < 'a' || strSource.at(i) > 'z'){
			return false;
		}
	}
	return true;
}
bool XJStringUtil::IsAZ(const string &strSource){
	if (strSource.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strSource.size();++i){
		if (strSource.at(i) < 'A' || strSource.at(i) > 'Z'){
			return false;
		}
	}
	return true;
}
bool XJStringUtil::IsAZaz(const string &strSource){
	if (strSource.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strSource.size();++i){
		if (strSource.at(i) < 'A' || strSource.at(i) > 'Z'){
			if (strSource.at(i) < 'a' || strSource.at(i) > 'z'){
				return false;
			}
		}
	}
	return true;
}
bool XJStringUtil::IsAZaz09(const string &strSource){
	if (strSource.size() == 0){
		return false;
	}
	unsigned long i;
	for (i=0;i<strSource.size();++i){
		if (strSource.at(i) < 'A' || strSource.at(i) > 'Z'){
			if (strSource.at(i) < 'a' || strSource.at(i) > 'z'){
				if (strSource.at(i) < '0' || strSource.at(i) > '9'){
					return false;
				}
			}
		}
	}
	return true;
}

} /* namespace XJUtilClass */
